/**
 * 数据资源
 */
import { ApiResult } from '@/api';
import request from '@/utils/request';
import { SysDataResource } from './model';

/**
 * 查询树形列表
 */
export async function listTree(params?: SysDataResource) {
  const res = await request.get<ApiResult<any>>('/sysDataResource/tree', {
    params
  });
  if (res.data.code === 200 && res.data.result) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询列表
 */
export async function list(params?: SysDataResource) {
  const res = await request.get<ApiResult<SysDataResource[]>>(
    '/sysDataResource/list',
    {
      params
    }
  );
  if (res.data.code === 200 && res.data.result) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加
 */
export async function add(data: SysDataResource) {
  const res = await request.post<ApiResult<unknown>>(
    '/sysDataResource/add',
    data
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改
 */
export async function update(data: SysDataResource) {
  const res = await request.post<ApiResult<unknown>>(
    '/sysDataResource/update',
    data
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function remove(id?: number | string) {
  const res = await request.post<ApiResult<unknown>>(
    '/sysDataResource/delete/',
    {
      id
    }
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
